package net.sansi.v3correctserver.task;

import cn.hutool.extra.spring.SpringUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import net.sansi.v3correctserver.constenum.AppHelper;
import net.sansi.v3correctserver.dto.analyse.PhotoAnalyseDto;
import net.sansi.v3correctserver.entity.analyse.PhotoAnalyseResult;
import net.sansi.v3correctserver.entity.image.RecImage;
import net.sansi.v3correctserver.entity.screen.CorrectArea;
import net.sansi.v3correctserver.entity.space.CorrectCnf;
import net.sansi.v3correctserver.service.camera.Camera;
import net.sansi.v3correctserver.service.photorec.RecService;
import net.sansi.v3correctserver.service.space.ProjectSpace;
import net.sansi.v3correctserver.service.takeshowphoto.TakeShowPhotoService;
import net.sansi.v3correctserver.util.CmmUtil;

/**
 * Created by Fang
 * create time : 2025/2/11
 * IDE : IntelliJ IDEA
 */

public class AutoAnalysePhotoTask extends V3Task<AutoAnalysePhotoTask, AutoAnalysePhotoTask.Progress> {

    private final PhotoAnalyseDto.RectAutoReq params;
    private final RecService recService;
    private final TakeShowPhotoService takeShowPhotoService;
    private final ProjectSpace projectSpace;
    private final CorrectCnf correctCnf;

    public AutoAnalysePhotoTask(PhotoAnalyseDto.RectAutoReq params) {
        super(5, new Progress());
        recService = SpringUtil.getBean(RecService.class);
        takeShowPhotoService = SpringUtil.getBean(TakeShowPhotoService.class);
        projectSpace = AppHelper.projectSpace();
        correctCnf = projectSpace.getCorrectCnf();
        progress.setCameraParams(params.getCameraParams());
        this.params = params;
    }


    @Override
    void runHandler() {
        // TODO 自动分析任务
        mock();
    }

    /**
     * 拍摄并分析一次
     *
     * @param cameraParams 相机参数
     * @return 分析结果
     */
    private PhotoAnalyseResult.Rect takeAndAnalyse(Camera.Params cameraParams) {
        int areaId = params.getAreaId();
        CorrectArea area = correctCnf.getArea(areaId);
        RecImage blackPhoto = takeShowPhotoService.takeBlack2Image(
                cameraParams,
                projectSpace.analyseBlackPhoto(areaId, params.getColorIdx())
        );
        RecImage ledPhoto = takeShowPhotoService.take2Image(
                area,
                params.getCameraParams().getBrightness(),
                params.getColorIdx(),
                correctCnf.getShowMode(),
                correctCnf.getShowSpan(),
                cameraParams,
                projectSpace.analysePhoto(areaId, params.getColorIdx()),
                0, 0
        );
        return recService.rectPhoto(ledPhoto, blackPhoto, params.getFence());
    }

    /**
     * mock函数
     * 模拟自动分析流程
     */
    void mock() {
        String cameraName = correctCnf.getCameraName();
        Camera camera = Camera.find(cameraName);
        Camera.Options options = camera.readOptions(false);
        for (int i = 0; i < 6; i++) {
            progress.setCount(i + 1);
            int tv = params.getCameraParams().getTv();
            params.getCameraParams().setTv(options.getTvs().get(i % options.getTvs().size()));
            CmmUtil.sleep(3000);
            notifyUpdateProgress();
        }
    }

    @Getter
    @Setter
    @ApiModel("相片分析进度")
    public static class Progress extends V3Task.Progress {
        @ApiModelProperty("拍摄次数")
        private int count = 0;

        @ApiModelProperty("总拍摄次数")
        private int totalCount = 6;

        @ApiModelProperty("最近一次相机参数")
        private Camera.Params cameraParams;
    }
}
